<template>
  <div>
    <p class="tip">键盘移动高亮行，设置 <table-api-link prop="keyboard-config"/>={isArrow: true} 启用方向键功能</p>

    <vhb-table
      border
      resizable
      show-overflow
      ref="xTable"
      height="300"
      :loading="loading"
      :row-config="{isCurrent: true}"
      :keyboard-config="{isArrow: true}">
      <vhb-column type="seq" width="100"></vhb-column>
      <vhb-column field="name" title="Name" sortable width="200"></vhb-column>
      <vhb-column field="age" title="Age" width="200"></vhb-column>
      <vhb-column field="sex" title="Sex" width="200"></vhb-column>
      <vhb-column field="rate" title="Rate" width="200"></vhb-column>
      <vhb-column field="region" title="Region" width="200"></vhb-column>
      <vhb-column field="time" title="Time" width="200"></vhb-column>
      <vhb-column field="address" title="Address" width="300" show-overflow></vhb-column>
      <vhb-column field="updateTime" title="UpdateTime" width="200"></vhb-column>
      <vhb-column field="createTime" title="CreateTime" width="200"></vhb-column>
    </vhb-table>

    <pre>
      <pre-code>
        | Arrow Up ↑ | 移动到高亮行的上一行 |
        | Arrow Down ↓ | 移动到高亮行的下一行 |
      </pre-code>
    </pre>

    <p class="demo-code">{{ $t('app.body.button.showCode') }}</p>

    <pre>
      <pre-code class="xml">{{ demoCodes[0] }}</pre-code>
      <pre-code class="javascript">{{ demoCodes[1] }}</pre-code>
    </pre>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      demoCodes: [
        `
        <vhb-table
          border
          resizable
          show-overflow
          ref="xTable"
          height="300"
          :row-config="{isCurrent: true}"
          :loading="loading"
          :keyboard-config="{isArrow: true}">
          <vhb-column type="seq" width="100"></vhb-column>
          <vhb-column field="name" title="Name" sortable width="200"></vhb-column>
          <vhb-column field="age" title="Age" width="200"></vhb-column>
          <vhb-column field="sex" title="Sex" width="200"></vhb-column>
          <vhb-column field="rate" title="Rate" width="200"></vhb-column>
          <vhb-column field="region" title="Region" width="200"></vhb-column>
          <vhb-column field="time" title="Time" width="200"></vhb-column>
          <vhb-column field="address" title="Address" width="300" show-overflow></vhb-column>
          <vhb-column field="updateTime" title="UpdateTime" width="200"></vhb-column>
          <vhb-column field="createTime" title="CreateTime" width="200"></vhb-column>
        </vhb-table>
        `,
        `
        export default {
          data () {
            return {
              loading: false
            }
          },
          created () {
            this.loading = true
            setTimeout(() => {
              this.mockList(600).then(data => {
                // 阻断 vue 对大数组的监听，大数据性能翻倍提升
                if (this.$refs.xTable) {
                  this.$refs.xTable.loadData(data)
                }
                this.loading = false
              })
            }, 300)
          },
          methods: {
            mockList (size) {
              return new Promise(resolve => {
                const list = []
                for (let index = 0; index < size; index++) {
                  list.push({
                    name: \`名称\${index}\`,
                    sex: '0',
                    num: 123,
                    age: 18,
                    num2: 234,
                    rate: 3,
                    address: 'shenzhen'
                  })
                }
                resolve(list)
              })
            }
          }
        }
        `
      ]
    }
  },
  created () {
    this.loading = true
    setTimeout(() => {
      this.mockList(600).then(data => {
        // 阻断 vue 对大数组的监听，大数据性能翻倍提升
        if (this.$refs.xTable) {
          this.$refs.xTable.loadData(data)
        }
        this.loading = false
      })
    }, 300)
  },
  methods: {
    mockList (size) {
      return new Promise(resolve => {
        const list = []
        for (let index = 0; index < size; index++) {
          list.push({
            name: `名称${index}`,
            sex: '0',
            num: 123,
            age: 18,
            num2: 234,
            rate: 3,
            address: 'shenzhen'
          })
        }
        resolve(list)
      })
    }
  }
}
</script>
